package tag;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import operate.DictOperate;
import operate.vo.Dict;
import util.StrUtil;
@SuppressWarnings("unchecked")
public class DictShow extends TagSupport{

	private static final long serialVersionUID = 1L;
	private String defaultValue="";//
	private String type;//请求类型
	private String selectName;//slect标签的名称同id属性值
	private String fun="";//js事件
	private String tip="";
	private String reg="";
	private String code="";
	

	@Override
	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		JspWriter out = pageContext.getOut();
		try {
			out.write(getDictTypeByCode(code));
		} catch (IOException e) {
			try {
				out.print("<font color=\"red\">标签出错</font>");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return super.doStartTag();
	}
	
	
	public String getDictTypeByCode(String code){
		List<Dict>  dicts=DictOperate.getDictBycode(code);
		StringBuffer sb=new StringBuffer();
		if(StrUtil.exist(reg)){
			reg="reg='"+reg+"'";
		}
		sb.append("<select name='"+selectName+"' id='"+selectName+"' "+fun+" "+reg+">");
		if(!StrUtil.exist(defaultValue)){
			sb.append("<option value=''>请选择</option>");
		}
		for (Dict dict : dicts) {
			sb.append("<option value='"+dict.getValue()+"' ");
			if(StrUtil.exist(defaultValue)&&defaultValue.equals(dict.getValue())){
				sb.append("selected='selected'");
			}
			sb.append(">"+dict.getName()+"</option>");
		}
		sb.append("</select>");
		return sb.toString();
	}
	
	
	public String getDefaultValue() {
		return defaultValue;
	}
	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getSelectName() {
		return selectName;
	}
	public void setSelectName(String selectName) {
		this.selectName = selectName;
	}
	public String getFun() {
		return fun;
	}
	public void setFun(String fun) {
		this.fun = fun;
	}
	public String getTip() {
		return tip;
	}
	public void setTip(String tip) {
		this.tip = tip;
	}
	public String getReg() {
		return reg;
	}


	public void setReg(String reg) {
		this.reg = reg;
	}


	public String getCode() {
		return code;
	}


	public void setCode(String code) {
		this.code = code;
	}

}
